package com.weme.home.adapter

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseDifferAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.weme.common.util.*
import com.weme.home.VH
import dev.kratos.api.user_server.v1.Users




class NewHomeAdapter(var callback: (Users) -> Unit) :
    BaseQuickAdapter<Users, VH>() {
    override fun onBindViewHolder(holder: VH, position: Int, item: Users?) {
        item?.apply {
            //holder.binding.imLock.setVisible(false)
            Log.e("homevideo", "刷新" + position + "个item" + item)
            this.mediaList.find { it.type.isTypeImg() }?.let {//1照片 2视频 3广告 4认证视频,5认证图片,6真人前置拍摄视频,7头像
                holder.binding.isAdView.setVisible(it.type != 3)

                when (it.type) {
                    1 ->{
                        if(context.isVip()){
                            holder.binding.myVideoView.getImageView().loadImageHome(it.url)
                        }else{
                            holder.binding.myVideoView.getImageView().loadImageHomeWithBlur(it.url)
                        }

                        initUserInfo(holder,this)
                    }
                    2 ->{
                        holder.binding.myVideoView.addVideoUrl(it.url)
                        initUserInfo(holder,this)
                    }
                    3 -> {
                            holder.binding.myVideoView.getImageView().loadImageHome(it.url)
                    }
                    else -> {
                    }
                }
            } ?: run {
                holder.binding.myVideoView.getImageView()
                    .setImageResource(com.example.resources.R.mipmap.icon_head_null)
            }
        }
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        return VH(parent)
    }
    private fun initUserInfo(holder: VH, item: Users){
        holder.binding.svgaCall.setOnClickListener {
            callback.invoke(item)
        }
        holder.binding.tvName.text = setNameAge(String.format(item.nickname + "," + item.age), 4)
        holder.binding.imHead.loadImageMin(item.headPic)
        val country = getCountryBean(item.countryCode)
        country?.let {
            holder.binding.imCountry.loadImageMin(it.countryFlag.toString())
        }
        when (item.onlineStatus) {
            1 -> {
                holder.binding.vCircle.setBackgroundResource(com.example.resources.R.drawable.online_dot)
                holder.binding.tvOnline.text =
                    context.resources.getString(com.example.resources.R.string.online)
            }

            2 -> {
                holder.binding.vCircle.setBackgroundResource(com.example.resources.R.drawable.busy_dot)
                holder.binding.tvOnline.text =
                    context.resources.getString(com.example.resources.R.string.busy)
            }

            else -> {
                holder.binding.vCircle.setBackgroundResource(com.example.resources.R.drawable.online_dot)
                holder.binding.tvOnline.text =
                    context.resources.getString(com.example.resources.R.string.online)
            }
        }
        holder.binding.svgaCall.playSVGAFromAssets("male_lobby_video_button")

    }

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
        super.onViewAttachedToWindow(holder)

        if((holder as VH).binding.svgaCall.visibility == View.VISIBLE){
            holder.binding.svgaCall.startAnimation() // 恢复播放
        }
    }

    override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
        super.onViewDetachedFromWindow(holder)
        if((holder as VH).binding.svgaCall.visibility == View.VISIBLE){
            holder.binding.svgaCall.stopAnimation() //  停止播放，释放资源
        }
    }


}